package com.huaxin.hxmoduleworkorder.controller;

import com.huaxin.hxmoduleworkorder.common.annotation.validation.ValidCustomerType;
import com.huaxin.hxmoduleworkorder.common.enums.CustomerType;
import com.huaxin.hxmoduleworkorder.dto.ResponseResult;
import com.huaxin.hxmoduleworkorder.dto.request.InspectionOrderItemBatchCreateReqDTO;
import com.huaxin.hxmoduleworkorder.dto.request.InspectionOrderItemBatchUpdateReqDTO;
import com.huaxin.hxmoduleworkorder.dto.request.InspectionOrderItemExistReqDTO;
import com.huaxin.hxmoduleworkorder.models.InspectionOrderItem;
import com.huaxin.hxmoduleworkorder.service.InspectionOrderItemService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "巡查工单项")
@RestController
@RequiredArgsConstructor
@RequestMapping("/v1/workorder/inspectionOrderItem")
public class InspectionOrderItemController {
    private final InspectionOrderItemService inspectionOrderItemService;

    @Operation(summary = "批量新增", description = "批量新增")
    @PostMapping("/batchCreate")
    public ResponseResult<String> batchCreate(@RequestBody @Valid InspectionOrderItemBatchCreateReqDTO in) {
        if (in == null) {
            return ResponseResult.parameterError(null, "请求参数不能为空");
        }
        return inspectionOrderItemService.batchCreate(in.getInspectionOrderItemList());
    }

    @Operation(summary = "批量更新", description = "批量更新")
    @PostMapping("/batchUpdate")
    public ResponseResult<String> batchUpdate(@RequestBody @Valid InspectionOrderItemBatchUpdateReqDTO in) {
        if (in == null) {
            return ResponseResult.parameterError(null, "请求参数不能为空");
        }
        return inspectionOrderItemService.batchUpdate(in.getInspectionOrderItemList());
    }

    @Operation(summary = "根据ID查询巡检项信息", description = "根据ID查询巡检项信息")
    @GetMapping("/getById")
    public ResponseResult<InspectionOrderItem> getByItemId(@Parameter(description = "工单检查项ID", name = "id") @RequestParam(name = "id", required = true) String id) {
        return inspectionOrderItemService.getByItemId(id);
    }

    @Operation(summary = "获取工单检查项列表", description = "获取工单检查项列表")
    @GetMapping("/list")
    public ResponseResult<List<InspectionOrderItem>> list(@Parameter(description = "工单ID", name = "orderId") @RequestParam(name = "orderId", required = true) String orderId,
                                                          @Parameter(description = "客户ID", name = "customerId") @RequestParam(name = "customerId", required = true) String customerId,
                                                          @Parameter(description = "配电箱ID", name = "cabinetId") @RequestParam(name = "cabinetId", required = true) String cabinetId) {
        return inspectionOrderItemService.listByOrder(orderId, customerId, cabinetId);
    }

    @Operation(summary = "判断工单是否有客户检查项", description = "判断工单是否有客户检查项")
    @PostMapping("/hasOrderItem")
    public ResponseResult<Boolean> hasOrderItem(@RequestBody @Valid InspectionOrderItemExistReqDTO in) {
        return inspectionOrderItemService.hasOrderItem(in);
    }

    @Operation(summary = "删除工单检查项", description = "删除工单检查项")
    @DeleteMapping("/deleteByOrderId")
    public ResponseResult<String> deleteByOrderId(@Parameter(description = "工单ID", name = "orderId") @RequestParam(name = "orderId", required = true) String orderId) {
        return inspectionOrderItemService.deleteByOrderId(orderId);
    }

    @Operation(summary = "删除客户工单检查项", description = "删除客户工单检查项")
    @DeleteMapping("/deleteByCustomerId")
    public ResponseResult<String> deleteByCustomerId(@Parameter(description = "工单ID", name = "orderId") @RequestParam(name = "orderId", required = true) String orderId,
                                                     @Parameter(description = "客户类型：commercial/residential", name = "customerType") @RequestParam(name = "customerType", required = true) @Valid @ValidCustomerType String customerType,
                                                     @Parameter(description = "客户ID", name = "customerId") @RequestParam(name = "customerId", required = true) String customerId) {
        return inspectionOrderItemService.deleteByCustomerId(orderId, CustomerType.of(customerType), customerId);
    }

    @Operation(summary = "删除配电房的工单检查项", description = "删除配电房的工单检查项")
    @DeleteMapping("/deleteByRoomId")
    public ResponseResult<String> deleteByRoomId(@Parameter(description = "工单ID", name = "orderId") @RequestParam(name = "orderId", required = true) String orderId,
                                                 @Parameter(description = "配电房ID", name = "roomId") @RequestParam(name = "roomId", required = true) String roomId) {
        return inspectionOrderItemService.deleteByRoomId(orderId, roomId);
    }

    @Operation(summary = "删除配电箱的工单检查项", description = "删除配电箱的工单检查项")
    @DeleteMapping("/deleteByCabinetId")
    public ResponseResult<String> deleteByCabinetId(@Parameter(description = "工单ID", name = "orderId") @RequestParam(name = "orderId", required = true) String orderId,
                                                    @Parameter(description = "配电房ID", name = "roomId") @RequestParam(name = "roomId", required = true) String roomId,
                                                    @Parameter(description = "配电箱ID", name = "cabinetId") @RequestParam(name = "cabinetId", required = true) String cabinetId) {
        return inspectionOrderItemService.deleteByCabinetId(orderId, roomId, cabinetId);
    }

    //今日巡查异常项数
    @Operation(summary = "今日巡查异常项数", description = "今日巡查异常项数")
    @DeleteMapping("/countTodayExceptionByCustomerId")
    public ResponseResult<Long> countTodayExceptionByCustomerId(@Parameter(description = "组织ID", name = "orgId") @RequestParam(name = "orgId", required = true) String orgId) {
        return inspectionOrderItemService.countTodayExceptionByCustomerId(orgId);
    }
}
